#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# @Time    : 2018/12/9 19:22
# @Author  : liujiantao
# @Site    : 
# @File    : mediaLine02.py
# @Software: PyCharm
from tiancheng.base.base_helper import *
train = pd.read_csv(features_base_path + "train_media_ftr.csv")
test = pd.read_csv(features_base_path + "test_media_ftr.csv")
print(train.shape)
print(test.shape)
tr_corr = train.corr()[tag_hd.Tag].reset_index()
# tr_corr
print(test.shape)
tr_corr = tr_corr[['Tag', 'index']].sort_values(by=tr_corr['Tag'].abs(), ascending=True)
print(tr_corr)
tr_corr.to_csv(features_base_path + "tr_corr.csv", index=False)
# col = tr_corr[tr_corr['Tag'].abs()>=0.01]['index'].tolist()
# print(col)
# col =
# train.to_csv(features_base_path + "train_data.csv", index=False)
# test.to_csv(features_base_path + "test_data.csv", index=False)
# print(list(col.values))

train = train.drop(['Tag'], axis=1).fillna(-1)
test = test.drop(['Tag'], axis=1).fillna(-1)
label = y['Tag']

test_id = sub['UID']

lgb_model = lgb.LGBMClassifier(boosting_type='gbdt', num_leaves=100, reg_alpha=3, reg_lambda=5, max_depth=-1,
                               n_estimators=5000, objective='binary', subsample=0.9, colsample_bytree=0.77,
                               subsample_freq=1, learning_rate=0.05,
                               random_state=1000, n_jobs=16, min_child_weight=4, min_child_samples=5, min_split_gain=0)
skf = StratifiedKFold(n_splits=5, random_state=2018, shuffle=True)
best_score = []

oof_preds = np.zeros(train.shape[0])
sub_preds = np.zeros(test_id.shape[0])

for index, (train_index, test_index) in enumerate(skf.split(train, label)):
    lgb_model.fit(train.iloc[train_index], label.iloc[train_index], verbose=50,
                  eval_set=[(train.iloc[train_index], label.iloc[train_index]),
                            (train.iloc[test_index], label.iloc[test_index])], early_stopping_rounds=30)
    best_score.append(lgb_model.best_score_['valid_1']['binary_logloss'])
    print(best_score)
    oof_preds[test_index] = lgb_model.predict_proba(train.iloc[test_index], num_iteration=lgb_model.best_iteration_)[:,
                            1]

    test_pred = lgb_model.predict_proba(test, num_iteration=lgb_model.best_iteration_)[:, 1]
    sub_preds += test_pred / 5
    # print('test mean:', test_pred.mean())
    # predict_result['predicted_score'] = predict_result['predicted_score'] + test_pred

m = tpr_weight_funtion(y_predict=oof_preds, y_true=label)
print(m)
sub = get_sub()
sub['Tag'] = sub_preds
sub.to_csv(sub_base_path + 'mediaLine_%s.csv' % str(m), index=False)


# path = sub_base_path
# trans_train = trans_train.merge(y, on='UID', how='left')


def find_wrong(trans_train, y, feature):
    black = (trans_train.groupby([feature])['Tag'].sum() / trans_train.groupby([feature])['Tag'].count()).sort_values(
        ascending=False)
    tag_count = trans_train.groupby([feature])['Tag'].count().reset_index()
    black = black.reset_index()
    black = black.merge(tag_count, on=feature, how='left')
    black = black.sort_values(by=['Tag_x', 'Tag_y'], ascending=False)
    return black


# Test_trans = trans_test
# Test_tag = pd.read_csv(sub_base_path+'mediaLine_%s.csv' % str(m))  # 测试样本
# rule_code = [  '5776870b5747e14e' ,'8b3f74a1391b5427' ,'0e90f47392008def' ,'6d55ccc689b910ee' ,'2260d61b622795fb' ,'1f72814f76a984fa' ,'c2e87787a76836e0' ,'4bca6018239c6201' ,'922720f3827ccef8' ,'2b2e7046145d9517' ,'09f911b8dc5dfc32' ,'7cc961258f4dce9c' ,'bc0213f01c5023ac' ,'0316dca8cc63cc17' ,'c988e79f00cc2dc0' ,'d0b1218bae116267' ,'72fac912326004ee' ,'00159b7cc2f1dfc8' ,'49ec5883ba0c1b0e' ,'c9c29fc3d44a1d7b' ,'33ce9c3877281764' ,'e7c929127cdefadb' ,'05bc3e22c112c8c9' ,'5cf4f55246093ccf' ,'6704d8d8d5965303' ,'4df1708c5827264d' ,'6e8b399ffe2d1e80' ,'f65104453e0b1d10' ,'1733ddb502eb3923' ,'a086f47f681ad851' ,'1d4372ca8a38cd1f' ,'29db08e2284ea103' ,'4e286438d39a6bd4' ,'54cb3985d0380ca4' ,'6b64437be7590eb0' ,'89eb97474a6cb3c6' ,'95d506c0e49a492c' ,'c17b47056178e2bb' ,'d36b25a74285bebb']
trans_train[tag_hd.Tag] = y[tag_hd.Tag]
black = find_wrong(trans_train, y, 'merchant')
rule_code_1 = black.sort_values(by=['Tag_x', 'Tag_y'], ascending=False).iloc[:50].merchant.tolist()
test_rule_uid = pd.DataFrame(trans_test[trans_test['merchant'].isin(rule_code_1)].UID.unique())
pred_data_rule = sub.merge(test_rule_uid, left_on='UID', right_on=0, how='left')
pred_data_rule['Tag'][(pred_data_rule[0] > 0)] = random.uniform(0.90, 1.0)
pred_data_rule[['UID', 'Tag']].to_csv(sub_base_path + 'sub_rule02.csv', index=False)
